package com.second.app.generic;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author GYZ
 * @Date 2023/10/11
 * <pre>
 *
 * </pre>
 */
public class GenericClass<T> {

    public static <T extends Number> double add1(T a, T b) {
        return a.doubleValue() + b.doubleValue();
    }

    public static <T extends Number> int add2(T a, T b) {
        return a.intValue() + b.intValue();
    }

    public <T> void printArray(T[] array) {
        for (T t : array) {
            System.out.println(t);
        }

    }

    public static <T> void print2List(List<? extends T> list) {
        for (T element : list) {
            System.out.println(element);
        }
    }

    public static <T> void add2ToList(List<? super T> list, T element) {
        list.add(element);
    }

    //上限Number
    public void printList(List<? extends Number> list) {
        for (Number number : list) {
            System.out.println(number);
        }
    }

    public <T> List<? super Number> addToList(List<? super Number> list, Number element) {
        list.add(element);
        return list;
    }

    public static void main(String[] args) throws ParseException {
        String date = "2023-10-07";
        SimpleDateFormat format;
        if (date.length() == 8) {
            format = new SimpleDateFormat("yyyyMMdd");
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        // 默认拉取当月的数据
        Date end = format.parse(date);
        Date start = getBeforeLastDayOfMonth(end);
        java.sql.Date startDate = new java.sql.Date(start.getTime());
        java.sql.Date endDate = new java.sql.Date(end.getTime());
        System.out.println(startDate + "::::" + endDate);
    }

    private static java.sql.Date getBeforeLastDayOfMonth(Date busDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(busDate);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date date = calendar.getTime();
        return new java.sql.Date(date.getTime());
    }
}
